package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.dao.TitleInformationMapper;
import com.hqyj.pojo.PersonnelTransfer;
import com.hqyj.pojo.TitleInformation;
import com.hqyj.service.TitleInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
@Service
public class TitleInformationServiceImpl extends ServiceImpl<TitleInformationMapper, TitleInformation> implements TitleInformationService {
    @Autowired(required = false)
    TitleInformationMapper titleInformationMapper;
    @Override
    public HashMap<String, Object> select(TitleInformation t) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //定义分页对象
        Page<TitleInformation> page = new Page<>(t.getPage(),t.getRow());
        //条件构造器
        QueryWrapper<TitleInformation> queryWrapper = new QueryWrapper<>();
        //根据id查询
        queryWrapper.eq(t.getId()!=null,"id",t.getId());
        //模糊查询
        queryWrapper.like(t.getEmployeeId()!=null&&!t.getEmployeeId().equals(""),"employee_id",t.getEmployeeId());
        queryWrapper.like(t.getName()!=null&&!t.getName().equals(""),"name",t.getName());


        //查询数据库
        Page<TitleInformation> eList =titleInformationMapper.selectPage(page,queryWrapper);
        //当前页集合
        map.put("list",eList.getRecords());
        //总页数
        map.put("totalPage",eList.getPages());
        //总条数
        map.put("total",eList.getTotal());
        //当前页
        map.put("curPage",t.getPage());
        if(t.getPage()==1){
            //上一页
            map.put("prePage",1);

        }else{
            map.put("prePage",t.getPage()-1);

        }
        if(t.getPage()==eList.getPages()){
            //下一页
            map.put("nextPage",eList.getPages());

        }else{
            map.put("nextPage",t.getPage()+1);

        }
        //每页显示条数
        map.put("row",t.getRow());

        return map;
    }

    @Override
    public HashMap<String, Object> add(TitleInformation t) {
        int insert = titleInformationMapper.insert(t);
        HashMap<String, Object> map = new HashMap<>();
        if(insert>0){
            map.put("info","添加成功");
        }else{
            map.put("info","添加失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(TitleInformation t) {
        int i = titleInformationMapper.updateById(t);
        HashMap<String, Object> map = new HashMap<String, Object>();
        if(i>0){
            map.put("info","修改成功");
        }else{
            map.put("info","修改失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer id) {
        int i = titleInformationMapper.deleteById(id);
        HashMap<String, Object> map = new HashMap<>();
        if(i>0){
            map.put("info","删除成功");
        }else{
            map.put("info","删除失败");
        }
        return map;
    }
}
